<div class="layui-fluid layui-anim febs-anim" id="febs-eximport" lay-title="导入导出">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <button class="layui-btn  layui-btn-sm layui-btn-primary" id="template-download"
                            shiro:hasPermission="railway:template">
                        <i class="layui-icon">&#xe82a;</i> 模板下载
                    </button>
                    <button class="layui-btn  layui-btn-sm layui-btn-primary" id="export"
                            shiro:hasPermission="railway:export">
                        <i class="layui-icon">&#xe7a8;</i> 导出当前页到Excel
                    </button>
                    <button class="layui-btn  layui-btn-sm layui-btn-success" id="exportAll"
                            shiro:hasPermission="railway:export">
                        <i class="layui-icon">&#xe7aa;</i> 导出所有数据到Excel
                    </button>
                    <button class="layui-btn  layui-btn-sm layui-btn-success" id="import"
                            shiro:hasPermission="railway:import">
                        <i class="layui-icon">&#xe7aa;</i> 导入Excel并分配给所有学生
                    </button>
                    <button class="layui-btn  layui-btn-sm layui-btn-success" id="importOne"
                            shiro:hasPermission="railway:import">
                        <i class="layui-icon">&#xe7aa;</i> 导入Excel并分配给一个学生
                    </button>
                    <button style="display: none" class="layui-btn  layui-btn-sm layui-btn-success" id="importOneHidden"
                            shiro:hasPermission="railway:import">
                        <i class="layui-icon">&#xe7aa;</i> 导入Excel并分配给一个学生
                    </button>
                    <input type="hidden" id="stuNoInput">
                    <table lay-filter="eximportTable" lay-data="{id: 'eximportTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="railway-status">
    {{#
    var status = {
    1: {title: '校对过', color: 'green'},
    0: {title: '未校对', color: 'volcano'}
    }[d.isSeg];
    }}
    <span class="layui-badge febs-tag-{{status.color}}">{{ status.title }}</span>
</script>
<script type="text/html" id="railway-eximport-option">
    <span shiro:lacksPermission="railway:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>
    <a lay-event="del" shiro:hasPermission="railway:delete"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'table', 'febs', 'upload'], function () {
        var $ = layui.jquery,
            febs = layui.febs,
            upload1 = layui.upload,
            upload2 = layui.upload,
            table = layui.table,
            $view = $('#febs-eximport'),
            tableIns;

        initTable();
        var stuNo="";
        upload1.render({
            elem: '#import',
            url: ctx + 'railway/import',
            accept: 'file',
            exts: 'xlsx', // 只能上传 .xlsx格式
            size: 10 * 1024, // kb
            before: function () {
                //alert("upload1")
                febs.view.loadBar.start();
            },
            done: function (res) {
                febs.view.loadBar.finish();
                if (res.code === 200) {
                    // 刷新 table
                    tableIns.reload({page: {curr: 1}});
                    var data = res.data;
                    // 弹出导入结果
                    febs.modal.open('导入结果', 'system/railway/eximport/result', {
                        area: $(window).width() <= 750 ? '95%' : '70%',
                        data: data
                    });
                }
            }, error: function (res) {
                //请求异常回调
                console.error(res);
                febs.view.loadBar.error();
                febs.alert.error('导入失败！');
            }
        });
        $view.on('click', '#importOne', function () {
            febs.modal.open('输入分配的学生学号', 'system/railway/inputStuNo', {
                area: $(window).width() <= 750 ? '50%' : '30%',
                btn2: function () {
                    layer.closeAll();
                }
            });
        });
        upload2.render({
            elem: '#importOneHidden',
            url: ctx + 'railway/importOne',
            accept: 'file',
            exts: 'xlsx', // 只能上传 .xlsx格式
            size: 10 * 1024, // kb
            data: {
                stuNo: function(){
                    return $('#stuNoInput').val();
                }
            },
            before: function () {
                //alert("upload2")
                febs.view.loadBar.start();
            },
            done: function (res) {
                febs.view.loadBar.finish();
                if (res.code === 200) {
                    // 刷新 table
                    tableIns.reload({page: {curr: 1}});
                    var data = res.data;
                    // 弹出导入结果
                    febs.modal.open('导入结果', 'system/railway/eximport/result', {
                        area: $(window).width() <= 750 ? '95%' : '70%',
                        data: data
                    });
                }
            }, error: function (res) {
                //请求异常回调
                console.error(res);
                febs.view.loadBar.error();
                febs.alert.error('导入失败！');
            }
        });

        table.on('tool(eximportTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;

            if (layEvent === 'del') {
                febs.modal.confirm('删除铁路文本数据', '确定删除该条数据？', function () {
                    deleteRailways(data.railwayId);
                });
            }
        });

        $view.on('click', '#template-download', function () {
            febs.download(ctx + 'railway/template', null, '标注文本导入模板.xlsx');
        });

        $view.on('click', '#export', function () {
            var params = {};
            params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
            params.pageNum = $view.find(".layui-laypage-em").next().html();
            febs.download(ctx + 'railway/excel', params, '标注文本Excel部分导出结果.xlsx');
        });

        $view.on('click', '#exportAll', function () {
            var params = {};
            febs.download(ctx + 'railway/excelExportAll', params, '标注文本Excel全部导出结果.xlsx');
        });

        function deleteRailways(railwaysIds) {
            febs.get(ctx + 'railway/delete/' + railwaysIds, null, function () {
                febs.alert.success('删除铁路文本成功');

                // 刷新 table
                tableIns.reload({page: {curr: 1}});
            });
        }
        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'eximportTable',
                url: ctx + 'railway/list',
                cols: [[
                    {field: 'fileName', title: '来源文件', width: 180},

                    {field: 'originContent', title: '原始文本'},

                    {title: '是否人工校对', templet: '#railway-status',width: 130},
                    {field: 'segWords', title: '人工校对后的分词结果'},
                    {field: 'createTime', title: '创建时间', width: 180, sort: true},
                    {title: '操作', toolbar: '#railway-eximport-option', width: 80}
                ]]
            });
        }

    })
</script>